// pages/cultrueConpous/myCultrueConpous/index.js
var common = require("../../../../public.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: '可使用', value: 0 },
      { text: '已使用', value: 1 },
      { text: '已失效', value: 2 },
    ],
    value1: 0,
    userInfo: '',
    token: "",
    active: '-1',
    discountCouponList: [],
    discountTypeList: [],
    noUseMoney: '', // 未使用卷总额
    adminCouponList: '',
    adminDiscountTypeList: '',
    isLoadingBusy: false,
    cityCouponList:[],
    cityCouponListss:[],
    tengZhou:[], //滕州券包集合
    actives:0 , //0是线上券 1线下券
    discountCouponLists:[], //线上标识
    Index:''
  },
  // 线上线下切换是啊金
  onChange(event) {
    this.setData({
      actives:event.detail.name,
      adminCouponList:[], //解决一个
    },()=>{
      this.fnStatusOone()
    })
  },
  toPlatformCoupons(e) {
    let dcId = e.currentTarget.dataset.item.acId
    wx.navigateTo({
      url: '/packageB/pages/cultureCoupons/platformCoupons/index?dcId=' + dcId + '&flag=0'
      + '&from=detail'
    })
  },
  onSwitch(e){
    var Index = e.detail  // 0 可使用 ，
    this.setData({
      Index
    },()=>{
      this.fnStatusOone()
    })
  },
  // 状态码封装一
  fnStatusOone(){
    if(this.data.Index==0){
      // 判断是线上还是线下  actives:0  //0是线上券 1线下券
      this.fnStatus(2) 
    }else if(this.data.Index==1){   //已使用 
      this.fnStatus(3) 
    }else if(this.data.Index==2){  //已失效
      this.fnStatus(0) 
    }
  },
  // 状态码
  fnStatus(statuss){
    if(this.data.actives==0){  //线上
      this.setData({
        discountCouponList:this.data.discountCouponLists.filter((x)=> x.status==statuss)
      })
   }else if(this.data.actives==1) {   //线下
     this.setData({
       adminCouponList:this.data.adminCouponLists.filter((x)=> x.status==statuss)
     })
   }
  },
  onLoad: function (options) {
    console.log(options,"djkdjkjkdsjkk")
  },
  onShow: function () {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
        }, function () {
          that.getMyUseFullCityDisCouponList()
        })
      }
    })
  },
  // 激活
  toJiHuo(e){
   console.log(e,"55555")
   let status =e.currentTarget.dataset.status.status
   var acId = e.currentTarget.dataset.status.acId
   var couponNo = e.currentTarget.dataset.status.couponNo
   var managerId =  e.currentTarget.dataset.status.managerId
   if(status==9){   //去激活
    wx.navigateTo({
      url:  
       '/packageB/pages/jihuo/index?acId=' + acId + 
      '&couponNo=' +couponNo
      + '&managerId='+managerId 
      + '&status=' + status 
      + '&tengZhou=' + JSON.stringify(this.data.tengZhou)
    })
    // 7
   }else if(status==7){  //拒绝编辑
    wx.navigateTo({
      url:  
       '/packageB/pages/jihuo/index?acId=' + acId + 
      '&couponNo=' +couponNo
      + '&managerId='+managerId 
      + '&status=' + status
      + '&tengZhou=' + JSON.stringify(this.data.tengZhou)
    })
   }
  },
  getMyUseFullCityDisCouponList() {
    this.setData({
      isLoadingBusy: true
    })
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.post('/cityDisCoupon/getMyUseFullCityDisCouponList', tempJson, function (res) {
      if (res.status === 200) {
        if (tempJson.couponType) {
          that.setData({
            discountCouponList: res.data,
            discountCouponLists:res.data,
            adminCouponList: [],
            cityCouponList:[],
            cityCouponListss:[]
            // discountTypeList: res.data.discountTypeList,
            // noUseMoney: res.data.noUseMoney && res.data.noUseMoney.toFixed(2),
          })


        } else { //初始化全部列表
          let newDiscountTypeList = []
          let newAdminDiscountTypeList = []
          //添加type   1==市券 ，2==平台券
          res.data.discountTypeList.map((v, i) => {
            newDiscountTypeList.push({
              ...v,
              type: 1
            })
          })
          res.data.adminDiscountTypeList.map((v, i) => {
            newAdminDiscountTypeList.push({
              ...v,
              type: 2
            })
          })
          var cityCouponList = []
          var cityCouponListss = []
          for (var i = 0; i < res.data.adminCouponList.length; i++) {
            // 过滤滕州券
            console.log(res.data.adminCouponList[i].tzAreaFlag)
            var tengZhou = res.data.adminCouponList.filter((x)=> x.tzAreaFlag==0 )
            // if (res.data.adminCouponList[i].isComposit == 1) { //isComposit ==1 不可叠加
            //   cityCouponList.push(res.data.adminCouponList[i])
            // } else {
            //   cityCouponListss.push(res.data.adminCouponList[i])
            // }
          }
          // console.log( cityCouponListss,"222222")
          // console.log( cityCouponList,"111111")
          that.setData({
            tengZhou,
            discountCouponList: res.data.discountCouponList,
            discountCouponLists:res.data.discountCouponList,
            discountTypeList: [...newDiscountTypeList, ...newAdminDiscountTypeList],
            adminCouponList: res.data.adminCouponList,
            adminCouponLists: res.data.adminCouponList,
            // cityCouponList,
            // cityCouponListss,
            noUseMoney: res.data.noUseMoney && res.data.noUseMoney.toFixed(2),
          },()=>{
            console.log(that.data.cityCouponList,"000000")
          })
        }
      } else {
        // 非200
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },
  toPlatformCouponsDetail(e) {
    console.log(e)
    if(e.currentTarget.dataset.item.type==4 ) return
    let id = e.currentTarget.dataset.item.acId
    wx.navigateTo({
      url: '/packageB/pages/cultureCoupons/platformCoupons/index?dcId='
       + id + '&from=detail'
      
       ,
    })
  },
  toCultureCouponsDetail(e) {
    // console.log(e)
    let id = e.currentTarget.dataset.item.dcId
    let no = e.currentTarget.dataset.item.couponNo
    let status =  e.currentTarget.dataset.item.status
    wx.navigateTo({
      url: '/packageB/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + id + '&from=detail' + '&no=' + no,
    })
  }
  
})